package com.um.swing.core.thread;

import cn.hutool.core.img.ImgUtil;
import com.um.swing.ThreadPool;
import com.um.swing.bean.ImageItem;
import com.um.swing.core.ImageJoinCore;
import com.um.swing.core.event.ExportEvent;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 名称：拼板导出 线程
 * 创建人：袁泉
 * 创建时间：2022年05月04日15:00:24
 */
public class ExportManageRunable implements Runnable {
    private List<ImageItem> data;
    private String targetPath;
    private ExportEvent finishCallback;
    private CountDownLatch countDownLatch;
    private boolean stopExport = false;
    private List<ExportRunable> runList;

    public ExportManageRunable(List<ImageItem> data, String targetPath, ExportEvent finishCallback) {
        this.data = data;
        this.targetPath = targetPath;
        this.finishCallback = finishCallback;
        this.countDownLatch = new CountDownLatch(1);
        this.runList = new ArrayList<>();

    }

    public void stopExport() {
        stopExport = true;
        for (ExportRunable item : runList) {
            item.stopExport();
        }
    }

    @Override
    public void run() {
        ExportRunable run = new ExportRunable(data, targetPath, countDownLatch, finishCallback);
        //TODO:提前兼容多线程导出逻辑
        runList.add(run);
        ThreadPool.execute(run);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finishCallback.onAllFinish();
    }
}
